package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.location.LocationExtend;
import com.yanqu.road.entity.location.LocationInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class LocationInfoDaoImpl extends TempDao {

    public Map<Integer, LocationInfo> getLocationInfoKeyMap() {

        Map<Integer, LocationInfo> infos = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_location";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                String regionType = rs.getString("regionType");
                String continent = rs.getString("continentShow");
                String country = rs.getString("countryShow");
                String world = rs.getString("worldShow");
                String cityStr = rs.getString("city");
                String cityIdStr = rs.getString("cityId");
                String[] cityArr = cityStr.split(";");
                String[] cityIdArr = cityIdStr.split(";");
                String province = rs.getString("province");
                if (cityArr.length != cityIdArr.length) {
                    logger.error("城市数量与id数量不一致。id: {}", rs.getInt("id"));
                    return null;
                }
                for (int i = 0; i < cityArr.length; i++) {
                    LocationInfo info = new LocationInfo();
                    info.setId(Integer.valueOf(cityIdArr[i]));
                    info.setCountry(country);
                    info.setProvince(province);
                    info.setCity(cityArr[i]);
                    info.setContinent(continent);
                    info.setCountryCode(regionType);
                    info.setWorld(world);
                    infos.put(info.getId(), info);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return infos;
    }

    public Map<Integer, LocationExtend> getLocationExtendMap() {
        Map<Integer, LocationExtend> infos = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_location_extend";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String language = rs.getString("language");
                String global = rs.getString("global");
                String continent = rs.getString("continent");
                String country = rs.getString("country");
                String province = rs.getString("province");
                String citys = rs.getString("city");
                String[] cityArr = citys.split("\\|");

                LocationExtend row = new LocationExtend();
                row.setId(id);
                row.setWorld(global);
                row.setContinent(continent);
                row.setCountry(country);
                row.setProvince(province);
                row.setLanguage(language);
                Set<String> citySet = new HashSet<>();
                for (int i = 0; i < cityArr.length; i++) {
                    citySet.add(cityArr[i]);
                }
                row.setCitys(citySet);
                infos.put(id, row);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return infos;
    }
}
